//
//  NoticeAdvertismentViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/17.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "NoticeAdvertismentViewController.h"

@interface NoticeAdvertismentViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (nonatomic,assign) int page;
@property (strong, nonatomic) NSMutableArray * dataArr;
@property (nonatomic,assign) BOOL isHeadFresh;

@end

@implementation NoticeAdvertismentViewController

static NSString * cellID = @"cell";
static NSString * NoticeAdvertismentTableViewCellID = @"NoticeAdvertismentTableViewCell";
static NSString * NoticeAdvertismentMoreTableViewCellID = @"NoticeAdvertismentMoreTableViewCell";
static NSString * NoticeAdvertismentNullTableViewCellID = @"NoticeAdvertismentNullTableViewCell";
static NSString * NoticeAdvertismentHeaderTableViewCellID = @"NoticeAdvertismentHeaderTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.title = @"通知公告";
    [self creatTableview];
    [self registCell];
    [self RequestData];
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark - 请求数据
-(void)RequestData
{
    NSString * url = [NSString stringWithFormat:@"/rest/gbNoticeBulletinController/getNoticeList?pageno=%d&pagesize=10&nbTitle=&nbType=",self.page];
    WeakSelf;
    [[HTTPClient shareClient] requestWithPath:url AndReturnBlock:^(NSDictionary *dic) {
        if (weakSelf.isHeadFresh) {
            [weakSelf.dataArr removeAllObjects];
        }
        NSArray * data = dic[@"data"];
        for (NSDictionary * dics in data) {
            NoticeAdvertismnetModel * model = [NoticeAdvertismnetModel modelWithDic:dics];
            [weakSelf.dataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        
    }];
    
}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[NoticeAdvertismentTableViewCell class] forCellReuseIdentifier:NoticeAdvertismentTableViewCellID];
    [self.tableview registerClass:[NoticeAdvertismentMoreTableViewCell class] forCellReuseIdentifier:NoticeAdvertismentMoreTableViewCellID];
    [self.tableview registerClass:[NoticeAdvertismentNullTableViewCell class] forCellReuseIdentifier:NoticeAdvertismentNullTableViewCellID];
    [self.tableview registerClass:[NoticeAdvertismentHeaderTableViewCell class] forCellReuseIdentifier:NoticeAdvertismentHeaderTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.backgroundColor = SFM242;
        tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
    
    // 下拉刷新
    WeakSelf;
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isHeadFresh = YES;
        weakSelf.page = 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_header endRefreshing];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 结束刷新
        weakSelf.isHeadFresh = NO;
        weakSelf.page += 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.dataArr.count - 1 == section) {
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
        line.backgroundColor = SFM242;
        return line;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.dataArr.count - 1 == section) {
        return 20;
    }
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        return 131;
    }else if(indexPath.row == 2)
    {
        return 44;
    }else
    {
        return 44;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArr == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        return cell;
    }
    NoticeAdvertismnetModel * model = [self.dataArr objectAtIndex:indexPath.section];
    if (indexPath.row == 1) {
        NoticeAdvertismentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NoticeAdvertismentTableViewCellID];
        [cell sendNoticeAdvertismnetModel:model];
        return cell;
    }else if(indexPath.row == 2){
        NoticeAdvertismentMoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NoticeAdvertismentMoreTableViewCellID];
        return cell;
    }else
    {
        NoticeAdvertismentHeaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NoticeAdvertismentHeaderTableViewCellID];
        cell.timeLabel.text = model.createDate;
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HtmlWebViewController * ctr = [[HtmlWebViewController alloc] init];
    NoticeAdvertismnetModel * model = [self.dataArr objectAtIndex:indexPath.section];
    ctr.url = [BASE_URL stringByAppendingString:[NSString stringWithFormat:@"/H5Html/Announcement.html?id=%@&tonken=%@",model.noticeID,SFMToken]];
    [self.navigationController pushViewController:ctr animated:YES];
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
